<template>
  <el-dialog
    :close-on-click-modal="false"
    width="45%"
    :title="title"
    :visible.sync="visible"
    :before-close="cancel"
    lock-scroll
    append-to-body
  >
    <el-form status-icon :model="datas" :rules="rule" @keyup.enter.native="save" ref="editForm">
      <el-row :gutter="10">
        <el-col :span="23">
          <el-form-item label="月份" :label-width="baseStyle.width">
            <el-date-picker
              v-model="month"
              type="month"
              placeholder="选择月"
              value-format="yyyy-MM"
              @change="changeDate"
              style="width:100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="23">
          <el-form-item prop="score" label="考核分数" :label-width="baseStyle.width">
            <el-input-number
              autofocus
              v-model="datas.score"
              :precision="1"
              :step="0.1"
              style="width:100%"
            ></el-input-number>
            <!-- <el-input v-model="datas.score" :readonly="false" autocomplete="off"></el-input> -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button :type="'primary'" :loading="loading" @click.native="save()">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui";

export default {
  name: "EditForm",
  props: {
    visible: Boolean,
    baseStyle: Object,
    title: String,
    datas: {
      type: Object,
      default: function () {
        return { month: "", score: "" };
      },
    },
    saveForm: Function,
  },
  data() {
    const rule = this.getRules();
    return {
      loading: false,
      month: "",
      rule,
    };
  },
  computed: {},
  created() {
    let date = new Date();
    let Year = date.getFullYear();
    let Day =
      date.getMonth() + 1 < 10
        ? `0${date.getMonth() + 1}`
        : date.getMonth() + 1;
    this.month = `${Year}-${Day}`;
  },
  methods: {
    changeDate(val) {
      this.month = val;
    },
    loadingClose() {
      this.loading = false;
    },
    cancel() {
      this.loading = false;
      this.$emit("update:visible", false);
      this.$refs.editForm.resetFields();
    },
    save() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.datas.month = this.month;
          this.$emit("saveForm", { ...this.datas });
        } else {
          return false;
        }
      });
    },
    isEmpty(value) {
      if (value === 0) {
        return false;
      }
      if (!value || value.length < 1) {
        return true;
      }
      return false;
    },
    validate(id, prop, value, des) {
      if (this.isEmpty(value)) {
        return {
          success: false,
          message: `请填写${des}`,
        };
      }
      return { success: true };
    },
    getRules() {
      return {
        score: [
          {
            // required: true,
            validator: (rule, value, callback) => {
              const result = this.validate(
                this.datas.score,
                "score",
                this.datas.score,
                "考核分数"
              );
              if (!result.success) {
                callback(new Error(result.message));
                return;
              }
              callback();
            },
            trigger: "blur",
          },
        ],
      };
    },
  },
};
</script>
<style lang="scss" scoped>
</style>